package com.pangyangyang.elasticsearch.plugin.handler;

import com.pangyangyang.elasticsearch.plugin.event.DataModifyEvent;
import com.pangyangyang.elasticsearch.plugin.river.MessageRiver;
import com.pangyangyang.elasticsearch.plugin.river.MessageRiverSettings;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * Created by yangwang on 2015/1/15.
 */
public class RepeaterHandler extends AbstractEventHandler implements EventHandler {

    public RepeaterHandler(Client client, MessageRiverSettings settings) {
        super(client, settings);
    }

    @Override
    public void handle(DataModifyEvent event) {
        String destination = event.getDataDestination();
        String type = event.getType().name();

        String jsonData = null;
        try {
            XContentBuilder builder = jsonBuilder().startObject()
                    .field("destination", destination)
                    .field("type", type)
                    .endObject();
            jsonData = builder.string();
        } catch (IOException e) {
            logger.error("input data failed.", e);
        }

        indexDocument(jsonData);
    }

    @Override
    protected void doStart() {

    }

    @Override
    public void stop() {

    }
}
